<?php
if(!defined('inside')) die('You do not have permission to execute this file directly.');

/**
* Routes class file.
*
* @author Federico Ramirez <fedekiller@gmail.com>
* @link http://code.google.com/p/akaikiwi/
* @copyright Copyright &copy; 2008-2011 Federico Ramirez
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
*/

/*
* This class manages routing
*/
class Routes {
	private $routes;

	function __construct() {
		$this->routes = array();
		// Route http://mysite.com/post/ANYTHING to http://mysite.com/welcome/showpost/ANYTHING
		//$this->routes['post/'] = 'welcome/showpost';
	}

	/**
	 * This method routes all custom routes to so they can be interpreted as normal routes.
	 */
	function router() {
		$path = $_SERVER['PHP_SELF'];
		if(substr($path, -1, 1) != '/') {
			$path .= '/';
		}
		
		$base_path = AkaiKiwi::loadLib('Config')->get_item('basePath');
		$path = explode($base_path . '/', $path);
		$path = $path[1];
				
		foreach($this->routes as $from => $to) {
			if(substr($from, -1, 1) != '/') {
				$from .= '/';				
			}
			
			if(strpos($path, $from) !== FALSE) {
				$_SERVER['PHP_SELF'] = $base_path . '/' . $to . '/' . substr($path, strpos($path, $from) + strlen($from));
			}
		}
	}
}
